const dependencies = {
    moduleA: ["moduleB", "moduleC"],
    moduleB: ["moduleC"],
    moduleC: [],
    moduleD: ["moduleA", "moduleB"]
}

// 输出 moduleC modelueB moduleA moduleD


function getLoadOrder(dependencies) {
    const visited = new  Set();
    const result = [];

    function dfs(module) {
        if (visited.has(module)) {
            return;
        }

        visited.add(module);
        const deps = dependencies[module] || [];
        for (const dep of deps) {
            dfs(dep);
        } 
        result.push(module);
    }

    for (const module in dependencies) {
        dfs(module);
    }

    return result;
}

console.log(getLoadOrder(dependencies));


function getLoadOrder2(dependencies) {
    const setArr = new Set();
    const result = [];

    function dfs(model) {
        if(setArr.has(model)) {
            return;
        }
        setArr.add(model);
        const deps = dependencies[model] || [];
        for(const dep of deps) {
            dfs(dep);
        }
        result.push(model);
    }

    for(const module in dependencies) {
        dfs(module)
    }
    return result;
}

console.log(getLoadOrder2(dependencies));